def set_eye_cup(win_size, shade = 1.0, eye_cup = None):
    ''' 
    Allows user to set the eye_cup with the joystick using psychopy.
    Inputs:
        win_size        : the (width, height) of screen in pixels
        shade           : 1.0 means black(fg) and white(bg)
        eye_cup         : an Eye_cup object, if None, then one is created.
    Outputs:
        eye_cup         : altered by user input.
    '''
    from psychopy import event, visual
    import copy
    import pygame
    import scipy
    import time

    dspeed = 20
    rotspeed = 2.0
    shrink_factor = 0.95
    grow_factor   = 1.05

    # open up a fullscreen window.
    myWin = visual.Window(win_size, pos=(0,-900), fullscr=0, 
                          allowGUI=0, rgb=(1.0)*shade)

    # have to initialize pygame
    pygame.init()
    # set up any joysticks if you have them
    pygame.joystick.init()
    count = pygame.joystick.get_count()
    
    if count > 0:
        j = pygame.joystick.Joystick(0)
        j.init()
        num_axes = j.get_numaxes()
        num_buttons = j.get_numbuttons()
        last_axes = [j.get_axis(i) for i in xrange(num_axes)] 

    fi = 0 # the focus list index
    focus_var = [eye_cup.streak, eye_cup.cup, eye_cup.nerve]
    focus_name = ['vs','ec','on']

    keep_looping = True
    while keep_looping:
        # see if any buttons have been pressed on the joystick.
        left = right = up = down = change_focus_up = scale_and_rotate = False
        axes = [j.get_axis(i) for i in xrange(num_axes)] 
        buttons  = [j.get_button(i) for i in xrange(num_buttons)]
        for i in xrange(len(axes)):
            if -0.1 < axes[i] and axes[i] < 0.1:
                axes[i] = 0
            if -1.1 < axes[i] and axes[i] < -0.9:
                axes[i] = -1
            if  0.9 < axes[i] and axes[i] < 1.1:
                axes[i] = 1
        # if the buttons or axes changed state this time around, print their new state
        for i in xrange(num_axes):
            if axes[i] != 0 and axes[i] != last_axes[i]:
                if i == 0 and axes[i] < 0:
                    left = True
                if i == 0 and axes[i] > 0:
                    right = True
                if i == 1 and axes[i] < 0:
                    up = True
                if i == 1 and axes[i] > 0:
                    down = True
            if buttons[i] !=0:
                if i == 0:
                    scale_and_rotate = True
                if i == 1 and last_buttons[1] == 0:
                    change_focus_up = True
        # save the previous states
        last_axes = axes
        last_buttons = copy.deepcopy(buttons)
     
        # handle joystick events
        fn = focus_name[fi]
        fv = focus_var[fi]

        if up and scale_and_rotate and fn != 'vs' :
            fv[2] *= grow_factor 
        elif up:
            fv[1] += dspeed

        if down and scale_and_rotate and fn != 'vs':
            fv[2] *= shrink_factor
        elif down:
            fv[1] -= dspeed

        if left and scale_and_rotate and fn == 'vs':
            fv[2] -= rotspeed
        elif left:
            fv[0] -= dspeed

        if right and scale_and_rotate and fn == 'vs':
            fv[2] += rotspeed
        elif right: 
            fv[0] += dspeed

        if change_focus_up:
            fi += 1
            while fi >= len(focus_var):
                fi -= len(focus_var)
        if change_focus_up and scale_and_rotate:
            break
                
        event.clearEvents()#get rid of other, unprocessed events
        
        # now set up the eye_cup and draw it.
        eye_cup.prep_for_drawing(myWin, shade=shade, force=True)
        eye_cup.draw(focus=fn)
        myWin.flip()#redraw the buffer

    # save a picture of what the eye coordinates look like.
    eye_cup.prep_for_drawing(myWin, shade=shade, opacity=1.0, force=True)
    eye_cup.draw()
    myWin.flip()
    eye_cup.draw()
    myWin.getMovieFrame()
    myWin.saveMovieFrames('eye_cup.jpg')
    myWin.close()
    pygame.quit()

    return eye_cup

        
# Run the application
if __name__ == '__main__':
    from Eye_cup import Eye_cup

    win_size = [1440,900]
    shade    = 1.0

    eye_cup = Eye_cup.from_eye_cup_file()
    set_eye_cup( win_size, shade=shade,  eye_cup=eye_cup )
    eye_cup.write_eye_cup_file()
    

